package nccloud.web.fct.ap.action;

import java.util.Map;
import nc.bs.framework.common.NCLocator;
import nc.itf.fct.ap.IApMaintain;
import nc.itf.fct.bill.IFctModifySpService;
import nc.vo.fct.ap.entity.AggCtApVO;
import nc.vo.fct.ap.entity.CtApBVO;
import nc.vo.fct.uitl.ArrayUtil;
import nc.vo.fct.util.FCTSagaUtil;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.AppContext;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.pubitf.fct.ap.ICopyActionProcessor;
import nccloud.web.fct.ap.event.after.impl.CSellOrigcurrencyHeadAfterEdithandler;



public class FctApCopyCardAction
  implements ICommonAction
{
  public Object doAction(IRequest request) {
    ExtBillCard retcard = null;

    
    try {
      String parameter = request.read();
      
      IJson json = JsonFactory.create();
      Map<String, String> param = (Map)json.fromJson(parameter, java.util.HashMap.class);
      String[] pks = new String[1];
      pks[0] = (String)param.get("pk");
      String pageId = (String)param.get("pageid");
      
      IApMaintain service = (IApMaintain)ServiceLocator.find(IApMaintain.class);
      
      AggCtApVO[] tAggCtApVO = service.queryCtApVoByIds(pks);
      if (tAggCtApVO == null || tAggCtApVO.length == 0) {
        
        tAggCtApVO = (AggCtApVO[])((IFctModifySpService)NCLocator.getInstance().lookup(IFctModifySpService.class)).queryCurrentVersionVosByPkOrigct(pks, false);
        
        if (ArrayUtil.isEmpty(tAggCtApVO)) {
          ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("2004003_0", "04020003-0473"));
        }
      } 
      AggCtApVO ctApVO = tAggCtApVO[0];

      
      for (CtApBVO itemvo : (CtApBVO[])ctApVO.getChildrenVO()) {
        itemvo.setIspealloced(UFBoolean.FALSE);
      }

      
      ctApVO.setCtApChangeVO(null);
      
      ctApVO.setCtApAllocationVO(null);
      
      FCTSagaUtil.clearSagaValue(tAggCtApVO);
      ICopyActionProcessor service1 = (ICopyActionProcessor)ServiceLocator.find(ICopyActionProcessor.class);
      service1.processVOAfterCopy(ctApVO);



      
      UFDate busidate = AppContext.getInstance().getBusiDate();
      ctApVO.getParentVO().setSubscribedate(busidate);
      ctApVO.getParentVO().setInvallidate(busidate);
      ctApVO.getParentVO().setValdate(busidate);
      ctApVO.getParentVO().setDbilldate(busidate);
      ctApVO.getParentVO().setVdef10("");

      
      CSellOrigcurrencyHeadAfterEdithandler handler = new CSellOrigcurrencyHeadAfterEdithandler();
      handler.setAggvo(ctApVO);
      handler.setValue(ctApVO.getParentVO().getCorigcurrencyid());
      handler.setKey("corigcurrencyid");
      handler.handle();
      
      retcard = FctApBillScaleDeal.dealArBillScale(ctApVO, pageId);
    }
    catch (Exception e) {
      
      ExceptionUtils.wrapException(ExceptionUtils.unmarsh(e));
    } 
    return retcard;
  }
}